package com.ssm.song.book.controller;

import com.ssm.song.book.domain.Category;
import com.ssm.song.book.domain.vo.BookVo;
import com.ssm.song.book.service.IBookService;
import com.ssm.song.book.util.Result;
import com.ssm.song.book.util.StatusCode;

import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author 宋伟宁
 * @Date 2023/10/16
 * @Version 1.0
 **/
@RestController
@RequestMapping("/book")
public class BookController {
    @Resource
    private IBookService bookService;

    @GetMapping("/books")
    public Result<List<BookVo>> books(){
        return this.bookService.findAll()==null
                ? new Result<>( StatusCode.ERROR,"查询失败",false)
                : new Result<>(StatusCode.OK,"查询成功",true,this.bookService.findAll());

    }

    /**
     * 添加出版社
     * @param category
     * @return
     */
    @PostMapping("/category")
    public Result addCategory(Category category){
        return  this.bookService.addCategory(category)
                ? new Result(StatusCode.OK,"新增成功",true)
                : new Result(StatusCode.ERROR,"新增失败",false);

    }

    @GetMapping("/category")
    public Result getCategorys(){
        return new Result(StatusCode.OK,"查询成功",true, this.bookService.getCategorys());
    }

    @DeleteMapping("/category/{cid}")
    public Result deleteCategory(@PathVariable int cid) {
        return this.bookService.deleteCategory(cid) ?
                new Result(StatusCode.OK,"删除成功",true):
                new Result(StatusCode.ERROR,"删除失败",false);
    }

    @PutMapping("/category")
    public Result updateCategory(@RequestBody Category category) {
        return this.bookService.updateCategory(category) ?
                new Result(StatusCode.OK,"修改成功",true):
                new Result(StatusCode.ERROR,"修改失败",false);
    }

}
